package com.iamk.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;

import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

import com.iamk.semanticsegment.Pixel;


public class GetInstances {
	static ArrayList<Attribute> listAttributes;
	public static List<Instances> getListInstance(String str) {
		List<Instances> list = new ArrayList<>();
		ArffLoader loader = new ArffLoader();
		Instances mInstances = null;
		File file = new File(str);
		try {
			for (int i = 0; i < file.listFiles().length; i++) {
				loader.setFile(new File(file.listFiles()[i].getPath()));
				mInstances = loader.getDataSet();
				list.add(mInstances);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return list;
	}
	// Create Instances to data train output of fcm
	public static Instances getInstancesToHash(HashMap<String, ArrayList<Pixel>> train, int  attribute, int cluster){
		// Writer header
		listAttributes=new ArrayList<Attribute>();
//		int attribute=5;
		Attribute temp;
		for(int i=0;i<attribute;i++){
			temp = new Attribute("f"+(i+1), Attribute.NUMERIC);
			listAttributes.add(temp);
		}
		// attibute class
		ArrayList<String> listCluster=new ArrayList<String>();
		for(int i=0;i<cluster;i++){
			if(((ArrayList<Pixel>)train.get(""+(i+1))).size()>0){
			listCluster.add(""+(i+1));}
		}
		temp=new Attribute("class", listCluster);
		listAttributes.add(temp);
		Instances mTestInstances=new Instances("Train Instances",listAttributes,0);
		mTestInstances.setClassIndex(attribute);
		// Writer data
		ArrayList<Pixel> tempPixels;
		for(int i=0;i<train.size();i++){
			tempPixels=train.get(""+(i+1));
			if(tempPixels.size()>0){
				for(int j=0;j<tempPixels.size();j++){
					mTestInstances.add(getInstance(tempPixels.get(j)));
				}
			}
		}
		return mTestInstances;
	}
	public static Instances getInstances(double[] features){
		Instances dataUnlabeled = new Instances("TestInstances", listAttributes, 0);
		features=ArrayUtils.add(features,0.0);
		DenseInstance newInst = new DenseInstance(1.0,features);
		newInst.setMissing(newInst.numAttributes()-1);
		dataUnlabeled.add(newInst);
		dataUnlabeled.setClassIndex(dataUnlabeled.numAttributes() - 1);        
//		double classif = ibk.classifyInstance(dataUnlabeled.firstInstance());
		return dataUnlabeled;
	}
	private  static DenseInstance getInstance(Pixel pixel) {
		double[] arrFeature = new double[ pixel.features.length];
		arrFeature=pixel.features;
		arrFeature=ArrayUtils.add(arrFeature, pixel.getLabel());
		DenseInstance newInstance = new DenseInstance(1.0, arrFeature);
//		newInstance.setValue(arrFeature.length+1, pixel.getLabel());
		return newInstance;
	}
//	public static void main(String[] args) {
//		GetInstances.getListInstance("D:\\University\\Ky10\\Code\\IAMK\\PreData\\Test");
//	}
}
